package com.eduagent.xwqeduagent.controller;

import com.eduagent.xwqeduagent.api.machineTranslation.api.TranslationApi;
import com.eduagent.xwqeduagent.common.BaseResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 机器翻译控制器
 */
@RestController
@RequestMapping("/translation")
@Tag(name = "翻译服务", description = "提供多语种文本翻译功能")
@Slf4j
public class TranslationController {

    @Autowired
    private TranslationApi translationApi;

    @Operation(summary = "自定义语言翻译", description = "将文本从源语言翻译为目标语言")
    @PostMapping("/translate")
    public BaseResponse<String> translate(
            @Parameter(description = "待翻译文本") @RequestParam String text,
            @Parameter(description = "源语言代码，如cn(中文)、en(英文)等") @RequestParam String from,
            @Parameter(description = "目标语言代码，如cn(中文)、en(英文)等") @RequestParam String to) {
        try {
            String result = translationApi.translate(text, from, to);
            return new BaseResponse<>(200, result, "翻译成功");
        } catch (Exception e) {
            log.error("翻译出错", e);
            return new BaseResponse<>(500, null, "翻译失败: " + e.getMessage());
        }
    }

    @Operation(summary = "中译英", description = "将中文文本翻译为英文")
    @PostMapping("/to-english")
    public BaseResponse<String> translateToEnglish(
            @Parameter(description = "中文文本") @RequestParam String text) {
        try {
            String result = translationApi.translateChineseToEnglish(text);
            return new BaseResponse<>(200, result, "翻译成功");
        } catch (Exception e) {
            log.error("中译英出错", e);
            return new BaseResponse<>(500, null, "翻译失败: " + e.getMessage());
        }
    }

    @Operation(summary = "英译中", description = "将英文文本翻译为中文")
    @PostMapping("/to-chinese")
    public BaseResponse<String> translateToChinese(
            @Parameter(description = "英文文本") @RequestParam String text) {
        try {
            String result = translationApi.translateEnglishToChinese(text);
            return new BaseResponse<>(200, result, "翻译成功");
        } catch (Exception e) {
            log.error("英译中出错", e);
            return new BaseResponse<>(500, null, "翻译失败: " + e.getMessage());
        }
    }
} 